<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/6
 * Time: 9:53
 */

namespace app\common\entity;


use think\Model;
use app\common\entity\Market;

class MywalletLog extends Model
{
    /**
     * @var string 对应的数据表名
     */
    protected $table = 'my_wallet_log';

    #获取列表
    public function getLog($uid, $page = 1, $limit = 5)
    {
        return $this::where('user_id', $uid)->page($page)->limit($limit)->order('create_time desc')->select();
    }

    public static function getTypes()
    {
        //1:钱包资产2:矿池资产3:可售资产4:可用额度
        return [
            1 => '福币',
            2 => '代金券',
            3 => '福卡',
            4 => '余额',
            5 => '积分',
        ];
    }

    public static function getWalletName($types)
    {

        switch ($types) {
            case 1:
                return 'bless';
            case 2:
                return 'coupon';
            case 3:
                return 'focas';
            case 4:
                return 'money';
            case 5:
                return 'credit';
            default:
                return '';
        }
    }
    public static function getChWalletName($types)
    {
        switch ($types) {
            case 1:
                return '福币';
            case 2:
                return '代金券';
            case 3:
                return '福卡';
            case 4:
                return '余额';
            case 5:
                return '积分';
            default:
                return '';
        }
    }
    public static function getAllTypes(){
        $arr = [
            1=> '系统充值',
            2=> '购买商品',
            3=> '取消购买',
            4=> '购物返利',
            5=> '签到',
            6=> '提现',
            7=> '会员充值',
            8=> '兑换',
            9=> '区域奖',
            10=> '直推注册',
            11=> '注册赠送',
            12=>'团队奖',
            13=>'代理游戏收益',
            14=>'抽奖奖励',
            15=>'房主收益',
            16=>'抽奖结束',
            17=>'团长奖',
            18=>'分红奖',
        ];
        return $arr;
    }
    public static function getOneTypes($types)
    {

        switch ($types) {
            case 1:
                return '系统充值';
            case 2:
                return '购买商品';
            case 3:
                return '取消购买';
            case 4:
                return '购物奖励';
            case 5:
                return '签到';
            case 6:
                return '提现';
            case 7:
                return '会员充值';
            case 8:
                return '兑换';
            case 9:
                return '区域奖';
            case 10:
                return '直推注册';
            case 11:
                return '注册赠送';
            case 12:
                return '团队奖';
            case 13:
                return '代理游戏收益';
            case 14:
                return '抽奖奖励';
            case 15:
                return '房主收益';
            case 16:
                return '抽奖结束';
            case 17:
                return '团长奖';
            case 18:
                return '分红奖';
            default:
                return '';
        }
    }



    /**
     * 添加记录
     * @param $uid              用户id
     * @param $number           数量
     * @param $wallet_type      1fbb,2fzf,3act
     * @param $remark           备注
     * @param $types            logtype
     * @param $status           1加2减
     * @param $from             来源会员
     * @return bool|int|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function addLog($uid, $number, $wallet_type, $remark, $types, $status,$from=0)
    {
        $money_type = self::getWalletName($wallet_type);
        if ($number == 0){
            return false;
        }

        $walletInfo = Mywallet::where('user_id', $uid)->find();

        if ($status == 2) {
            if($walletInfo[$money_type] - $number < 0){
                return false;
            }
            Mywallet::where('user_id',$uid)->update([
                $money_type=>$walletInfo[$money_type] - $number,
                'update_time'=>time()
            ]);
            $ins = self::insert([
                'user_id' => $uid,
                'number' => $number,
                'old' => $walletInfo[$money_type]??0,
                'new' => $walletInfo[$money_type] - $number,
                'remark' => $remark,
                'types' => $types,
                'money_type'=>$wallet_type,
                'status' => $status,
                'from' => $from,
                'create_time' => time()
            ]);
        } else {
            Mywallet::where('user_id',$uid)->update([
                $money_type=>$walletInfo[$money_type]>0? $walletInfo[$money_type] + $number:$number,
                'update_time'=>time()
            ]);
            $ins = self::insert([
                'user_id' => $uid,
                'number' => $number,
                'old' => $walletInfo[$money_type]??0,
                'new' => $walletInfo[$money_type]>0? $walletInfo[$money_type] + $number:$number,
                'remark' => $remark,
                'types' => $types,
                'money_type'=>$wallet_type,
                'status' => $status,
                'from' => $from,
                'create_time' => time()
            ]);
        }
        return $ins;
    }


    /**
     * 锁仓到gyg
     * @param $uid
     * @param $number
     * @param $remark
     * @param $types
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function lockToGyg($uid,$number,$remark,$types){
        $lock = Mywallet::where('user_id',$uid)->value('lock');
        if($lock < $number){
            $number = $lock;
        }
        if(self::addLog($uid, $number, 2, $remark, $types, 2)){
            
            $gyg = self::addLog($uid, $number, 1, $remark, $types, 1);
            if($gyg){
                return true;
            }
        }
        return false;
    }


    static public function activity($uid,$number){
        $user = User::where('id',$uid)->find();
        if($user['pid'] == 0){
            return 4;//没有父级的不配给予返佣
        }
        #直推
        $zhitui = User::where('id',$user['pid'])->find();
        if(!$zhitui){
            return 1;//没有父级就没有新生活
        }
        if($zhitui['is_close'] == 1){
            $num = bcmul($number,bcmul(UserLevelSend::getProfit($zhitui['level'],'direct'),0.01,4),4);
            self::lockToGyg($zhitui['id'],$num,'直推加速',2);
        }
        #间推
        $jiantui =  User::where('id',$zhitui['pid'])->find();
        if(!$jiantui){
            return 2;//没有爷级就算了
        }
        if($jiantui['is_close'] == 1){
            $num = bcmul($number,bcmul(UserLevelSend::getProfit($jiantui['level'],'indirect'),0.01,4),4);
            self::lockToGyg($jiantui['id'],$num,'间推加速',2);
        }
        return 3;
    }

    static public function register($uid){
        if($uid == 0){
            return true;
        }
        #直推
        $zhitui = User::where('id',$uid)->find();
        if(!$zhitui){
            return 1;//没有父级就没有新生活
        }

        if($zhitui['is_close'] == 1){
            self::addLog($zhitui['id'], Config::getValue('lock_zhitui_register'), 2, '直推注册赠送', 9, 1);
        }

        #间推
        $jiantui = User::where('id',$zhitui['pid'])->find();

        $jiantui =  User::where('id',$zhitui['pid'])->find();
        if(!$jiantui){
            return 2;//没有爷级就算了
        }
        if($jiantui['is_close'] == 1){
            self::addLog($jiantui['id'], Config::getValue('lock_jiantui_register'), 2, '间推注册赠送', 9, 1);
        }
        #团队
        $users = User::select();
        $parents = User::fanTree($users,$jiantui['pid']);
        foreach($parents as $k=>$v){
            if($v['is_close'] == 1){
                self::addLog($v['id'], Config::getValue('lock_temp_register'), 2, '团队注册赠送', 9, 1);
            }
        }
        return 3;
    }
}